package com.hellojavaweb.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;

/**
 * Created by l00383533 on 2017/1/10.
 */
public class LoginServlet extends javax.servlet.http.HttpServlet {

    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

//        String username = request.getParameter("username");
//        String pass = request.getParameter("pass");
//        System.out.println(username);
//        System.out.println(pass);
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////
        //接收用户名和密码
        String username=null;
        String pass = null;

        Connection con = null;
        Statement sta = null;
        ResultSet rs = null;

        try {
            username = request.getParameter("username");
            pass = request.getParameter("pass");

            if (username == null || username.equals("") || pass == null || pass.equals("")) {
                ServletContext application = request.getServletContext();
                application.setAttribute("message", "用户名或密码为空");

                response.sendRedirect("err_dtt.jsp");
                return;
            }

            //加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");

            //创建数据库url
            String dbIp = "localhost";//数据库地址
            String dbPort = "3306";//默认数据库端口
            String dbSchema = "test";//数据库名

            String url = "jdbc:mysql://" + dbIp + ":" + dbPort + "/" + dbSchema;
            String user = "root";
            String passd = "daitt";

            //建立连接
            try {
                con = DriverManager.getConnection(url,user,passd);
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("1");
            }

            //创建statement
            sta = con.createStatement();

            rs = sta.executeQuery("select pass from t_user_info where username='"+username+"'limit 1");

            if(rs.next()){
                //说明用户存在
                String dbPasswd = rs.getString(1);
                if(dbPasswd.equals(pass)){
                    //合法
                    //将用户信息存入session
                    HttpSession session = request.getSession();
                    session.setAttribute("username", username);


                    //跳到下一个页面【servlet提供了两种：sendredirect转向，forward转发】
                    //sendredirect 的 url 应该这样：/web应用名/servlet url
                    response.sendRedirect("main_dtt.jsp");
                }else{
                    //跳回登录页面

                    ServletContext application = request.getServletContext();
                    application.setAttribute("message", "密码错误");

                    response.sendRedirect("err_dtt.jsp");
                }
            }else{

                ServletContext application = request.getServletContext();
                application.setAttribute("message", "用户名不存在");

                response.sendRedirect("err_dtt.jsp");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            try {
                if(null != rs){
                    rs.close();
                }
                if(null != sta){
                    sta.close();
                }
                if(null != con){
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////

//          else {
//            if (username.equals("admin") && pass.equals("admin")) {
//                //            response.getWriter().append("success");
//                //添加参数
////                ServletContext application = request.getServletContext();
////                application.setAttribute("username", username);
//                HttpSession session = request.getSession();
//                session.setAttribute("username", username);
//                response.sendRedirect("main_dtt.jsp");
//            } else {
//                response.getWriter().append("failed");
//            }
//        }
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

//        response.getWriter().append("This is from here");

        //添加参数
//        ServletContext application = request.getServletContext();
//        application.setAttribute("username",);


        //直接跳转到index.jsp页面
//        response.sendRedirect("main.jsp");

        //转发请求到别的页面
//        RequestDispatcher errorPage = request.getRequestDispatcher("indexServlet");
//        errorPage.forward(request, response);
        doPost(request ,response);
    }
}
